<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Keuangan_yayasan extends MX_Controller {
    
  function __construct() {
//    $this->load->library('manimage');
    $this->load->model('frm/mfrm');
    $this->menu = $this->cek();
  }
  
  function addrow_account($debit_akun, $da, $debit_price, $debit){
    $no = rand(100, 100000);
    $isi = $this->form_eksternal->form_autocomplate($debit_akun."[]", $da."[]", $debit."_".$no, $da."_".$no, site_url("frm/accounting/auto-account"), "", "", "", "");
    $isi2 = $this->form_eksternal->form_input_price($debit."_".$no."set", $debit_price."[]", "");
    print <<<EOD
    <tr id="tr_{$no}">
      <td>{$isi}</td>
      <td>{$isi2}</td>
   <td><a href='#' class='remove-element' onclick="del_row_tambahan('tr_{$no}')" title='Remove' >Remove</a></td></tr>
EOD;
    die;
  }
	
	public function master_account_pajak(){
    $data = $this->global_models->get("frm_account_pajak");
    $this->template->build("accounting/master-account-pajak",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'frm/accounting/master-account-pajak',
            'title'       => lang("frm_account_pajak"),
            'data'        => $data
          ));
    $this->template
      ->set_layout('default')
      ->build("accounting/master-account-pajak");
	}
	
	public function journal_pajak($id_frm_journal){
    $data = $this->global_models->get_query("
      SELECT A.*, B.saldo
      FROM frm_account_pajak AS A
      LEFT JOIN frm_neraca_pajak AS B ON (A.id_frm_account_pajak = B.id_frm_account_pajak AND B.id_frm_journal = {$id_frm_journal})
      ");
    $this->template->build("accounting/journal-pajak",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'frm/accounting/journal',
            'title'       => lang("frm_journal_pajak"),
            'data'        => $data,
            'id_frm_journal' => $id_frm_journal
          ));
    $this->template
      ->set_layout('default')
      ->build("accounting/journal-pajak");
	}
	
	public function add_master_account_pajak($id_frm_account_pajak = 0){
    if($this->input->post(NULL)){
      $pst = $this->input->post(NULL);
      if($pst['id_detail']){
        $kirim = array(
            "title"               => $pst['title'],
            "nomor"               => $pst['nomor'],
            "pos"                 => $pst['pos'],
            "note"                => $pst['note'],
            "update_by_users"     => $this->session->userdata("id")
        );
        $id_frm_account_pajak = $this->global_models->update("frm_account_pajak", array("id_frm_account_pajak" => $pst['id_detail']),$kirim);
      }
      else{
        $kirim = array(
            "title"               => $pst['title'],
            "nomor"               => $pst['nomor'],
            "pos"                 => $pst['pos'],
            "note"                => $pst['note'],
            "create_by_users"     => $this->session->userdata("id"),
            "create_date"         => date("Y-m-d H:i:s")
        );
        $id_frm_account_pajak = $this->global_models->insert("frm_account_pajak", $kirim);
      }
      if($id_frm_account){
        redirect("frm/accounting/master-account-pajak/1/Berhasil");
      }
      else{
        redirect("frm/accounting/master-account-pajak/2/Gagal");
      }
    }
    else{
      $detail = $this->global_models->get("frm_account_pajak", array("id_frm_account_pajak" => $id_frm_account_pajak));
      $this->template->build("accounting/add-master-account-pajak",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'frm/accounting/master-account-pajak',
              'title'       => lang("frm_account_pajak"),
              'detail'      => $detail,
            ));
      $this->template
        ->set_layout('default')
        ->build("accounting/add-master-account-pajak");
    }
	}
	
	public function add_journal_pajak($id_frm_journal, $id_frm_account_pajak){
    if($this->input->post(NULL)){
      $pst = $this->input->post(NULL);
      if($this->global_models->get_field("frm_neraca_pajak", "id_frm_neraca_pajak", 
              array("id_frm_journal" => $pst['id_frm_journal'], "id_frm_account_pajak" => $pst['id_frm_account_pajak']))){
        $kirim = array(
            "saldo"               => $this->global_models->string_to_number($pst['price']),
            "update_by_users"     => $this->session->userdata("id")
        );
        $id_frm_neraca_pajak = $this->global_models->update("frm_neraca_pajak", 
                array("id_frm_journal" => $pst['id_frm_journal'], "id_frm_account_pajak" => $pst['id_frm_account_pajak']),$kirim);
      }
      else{
        $kirim = array(
            "id_frm_journal"       => $pst['id_frm_journal'],
            "id_frm_account_pajak" => $pst['id_frm_account_pajak'],
            "saldo"               => $this->global_models->string_to_number($pst['price']),
            "pos"                 => 1,
            "create_by_users"     => $this->session->userdata("id"),
            "create_date"         => date("Y-m-d H:i:s")
        );
        $id_frm_neraca_pajak = $this->global_models->insert("frm_neraca_pajak", $kirim);
      }
      
      redirect("frm/accounting/journal-pajak/".$id_frm_journal);
    }
    else{
      $price = $this->global_models->get_field("frm_neraca_pajak", "saldo", 
              array("id_frm_account_pajak" => $id_frm_account_pajak, "id_frm_journal" => $id_frm_journal));
      $this->template->build("accounting/add-journal-pajak",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'frm/accounting/journal',
              'title'       => lang("frm_journal_pajak"),
              'price'      => $price,
              'id_frm_account_pajak'  => $id_frm_account_pajak,
              'id_frm_journal'  => $id_frm_journal,
            ));
      $this->template
        ->set_layout('default')
        ->build("accounting/add-journal-pajak");
    }
	}
	
	public function add_journal($id_keuangan_yayasan_journal = 0){
    $bulan = array(
        '01'      => 'Januari',
        '02'      => 'Februari',
        '03'      => 'Maret',
        '04'      => 'April',
        '05'      => 'Mei',
        '06'      => 'Juni',
        '07'      => 'Juli',
        '08'      => 'Agustus',
        '09'      => 'September',
        '10'      => 'Oktober',
        '11'      => 'Nopember',
        '12'      => 'Desember',
    );
    if($this->input->post(NULL)){
      $pst = $this->input->post(NULL);
      if($pst['id_detail']){
        $kirim = array(
            "title"               => $bulan[$pst['month']]." ".$pst['year'],
            "month"               => $pst['month'],
            "year"                => $pst['year'],
            "sort"                => $pst['year']."-".$pst['month']."-01",
            "update_by_users"     => $this->session->userdata("id")
        );
        $id_keuangan_yayasan_journal = $this->global_models->update("keuangan_yayasan_journal", array("id_keuangan_yayasan_journal" => $pst['id_detail']),$kirim);
      }
      else{
        $kirim = array(
            "title"               => $bulan[$pst['month']]." ".$pst['year'],
            "month"               => $pst['month'],
            "year"                => $pst['year'],
            "sort"                => $pst['year']."-".$pst['month']."-01",
            "create_by_users"     => $this->session->userdata("id"),
            "create_date"         => date("Y-m-d H:i:s")
        );
        $id_keuangan_yayasan_journal = $this->global_models->insert("keuangan_yayasan_journal", $kirim);
      }
      if($id_keuangan_yayasan_journal){
        $this->session->set_flashdata('success', 'Data tersimpan');
      }
      else{
        $this->session->set_flashdata('notice', 'Data tidak tersimpan');
      }
      redirect("keuangan-yayasan/journal");
    }
    else{
      $detail = $this->global_models->get("frm_journal", array("id_frm_journal" => $id_frm_journal));
      
      $this->template->build("add-journal",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'keuangan-yayasan/journal',
              'title'       => lang("frm_journal"),
              'detail'      => $detail,
              'bulan'       => $bulan
            ));
      $this->template
        ->set_layout('default')
        ->build("add-journal");
    }
	}
	
	public function add_journal_transaksi($id_keuangan_yayasan_journal, $id_keuangan_yayasan_journal_log = 0){
    if($this->global_models->get_field("keuangan_yayasan_journal", "status", array("id_keuangan_yayasan_journal" => $id_keuangan_yayasan_journal)) == 2)
      redirect("keuangan-yayasan/journal-log/".$id_keuangan_yayasan_journal);
    
    $bulan = array(
        '01'      => 'Januari',
        '02'      => 'Februari',
        '03'      => 'Maret',
        '04'      => 'April',
        '05'      => 'Mei',
        '06'      => 'Juni',
        '07'      => 'Juli',
        '08'      => 'Agustus',
        '09'      => 'September',
        '10'      => 'Oktober',
        '11'      => 'Nopember',
        '12'      => 'Desember',
    );
    if($this->input->post(NULL)){
      $pst = $this->input->post(NULL);
      
//      $saldo_debit
      foreach($pst['debit_price'] AS $kdp => $dp){
        $just_debit = $this->global_models->string_to_number($dp);
        $saldo_debit += $just_debit;
        
        $debit[] = $just_debit;
        $debit_da[] = $pst['da'][$kdp];
      }
      foreach($pst['kredit_price'] AS $kkp => $kp){
        $just_kredit = $this->global_models->string_to_number($kp);
        $saldo_kredit += $just_kredit;
        
        $kredit[] = $just_kredit;
        $kredit_da[] = $pst['ka'][$kkp];
      }
      if($saldo_debit == $saldo_kredit){
        if($pst['id_detail']){
          $kirim = array(
              "id_frm_journal"      => $id_frm_journal,
              "notransaksi"         => $pst['notransaksi'],
              "saldo"               => $saldo_debit,
              "status"              => 1,
              "tanggal"             => $pst['tanggal'],
              "note"                => $pst['note'],
              "update_by_users"     => $this->session->userdata("id")
          );
          $id_frm_journal_log = $this->global_models->update("frm_journal_log", array("id_frm_journal_log" => $pst['id_detail']),$kirim);
          
          foreach($pst['debit_price'] AS $key => $dp){
            $kirim_log_account[] = array(
                "id_frm_account"    => $pst['da'][$key],
                "id_frm_journal"    => $id_frm_journal,
                "id_frm_journal_log" => $pst['id_detail'],
                "saldo"             => $this->global_models->string_to_number($dp),
                "pos"               => 1,
                "create_by_users"     => $this->session->userdata("id"),
                "create_date"         => date("Y-m-d H:i:s")
            );
          }
          foreach($pst['kredit_price'] AS $key => $kp){
            $kirim_log_account[] = array(
                "id_frm_account"    => $pst['ka'][$key],
                "id_keuangan_yayasan_journal"    => $id_frm_journal,
                "id_keuangan_yayasan_journal_log"    => $pst['id_detail'],
                "saldo"             => $this->global_models->string_to_number($kp),
                "pos"               => 2,
                "create_by_users"     => $this->session->userdata("id"),
                "create_date"         => date("Y-m-d H:i:s")
            );
          }
          $this->global_models->delete("frm_journal_log_account", array("id_frm_journal" => $id_frm_journal, "id_frm_journal_log" => $pst['id_detail']));
          $this->global_models->insert_batch("frm_journal_log_account", $kirim_log_account);
          $this->session->set_flashdata('success', 'Data tersimpan');
            
        }
        else{
          $kirim = array(
              "id_keuangan_yayasan_journal"      => $id_keuangan_yayasan_journal,
              "notransaksi"         => $pst['notransaksi'],
              "saldo"               => $saldo_debit,
              "tanggal"             => $pst['tanggal'],
              "note"                => $pst['note'],
          );
          $id_keuangan_yayasan_journal_log = $this->mfrm->create_journal_log_yayasan($kirim);
        
          if($id_keuangan_yayasan_journal_log){
            
            $this->mfrm->create_journal_log_account_yayasan($id_keuangan_yayasan_journal, $id_keuangan_yayasan_journal_log, $debit, $debit_da, $kredit, $kredit_da);
            
            $this->session->set_flashdata('success', 'Data tersimpan');
          }
          else{
            $this->session->set_flashdata('notice', 'Data tidak tersimpan');
          }
        }
      }
      else{
        $this->session->set_flashdata('notice', 'Saldo debit dan kredit tidak sama');
      }
      redirect("keuangan-yayasan/journal-log/{$id_keuangan_yayasan_journal}");
    }
    else{
      $detail = $this->global_models->get("keuangan_yayasan_journal_log", array("id_keuangan_yayasan_journal_log" => $id_keuangan_yayasan_journal_log));
      if($detail[0]->id_keuangan_yayasan_journal_log){
        $keuangan_yayasan_journal_log_accoutn = $this->global_models->get_query("
          SELECT A.*, B.title AS account
          FROM keuangan_yayasan_journal_log_account AS A
          LEFT JOIN keuangan_yayasan_account AS B ON A.id_keuangan_yayasan_account = B.id_keuangan_yayasan_account
          WHERE id_keuangan_yayasan_journal_log = {$id_keuangan_yayasan_journal_log}");
        $no_debit = 0;
        $no_kredit = 0;
        foreach($keuangan_yayasan_journal_log_accoutn as $fjla){
          if($fjla->pos == 1){
            $tr['debit'][] = 'trdebit_'.$no_debit;
            $string['debit'][] = $fjla->account;
            $price_value['debit'][] = $fjla->saldo;
            $value_id['debit'][] = $fjla->id_frm_account;
            $no_debit++;
          }
          else{
            $tr['kredit'][] = 'trkredit_'.$no_kredit;
            $string['kredit'][] = $fjla->account;
            $price_value['kredit'][] = $fjla->saldo;
            $value_id['kredit'][] = $fjla->id_frm_account;
            $no_kredit++;
          }
        }
      }
      else{
        $tr = array(
            "debit"   => array('trdebit_1'),
            "kredit"  => array('trkredit_1')
        );
      }
      
      $this->template->build("add-journal-log",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'keuangan-yayasan/journal',
              'title'       => lang("frm_journal_transaksi"),
              'data'        => $detail,
              'bulan'       => $bulan,
              'id_keuangan_yayasan_journal' => $id_keuangan_yayasan_journal,
              'tr'          => $tr,
              'string'      => $string,
              'price_value' => $price_value,
              'value_id'    => $value_id,
            ));
      $this->template
        ->set_layout('default')
        ->build("add-journal-log");
    }
	}
	
	public function add_journal_transaksi_penyesuaian($id_keuangan_yayasan_journal, $id_keuangan_yayasan_journal_log_penyesuaian = 0){
    if($this->global_models->get_field("keuangan_yayasan_journal", "status", array("id_keuangan_yayasan_journal" => $id_keuangan_yayasan_journal)) == 2)
      redirect("keuangan-yayasan/journal-log-penyesuaian/".$id_keuangan_yayasan_journal);
    
    $bulan = array(
        '01'      => 'Januari',
        '02'      => 'Februari',
        '03'      => 'Maret',
        '04'      => 'April',
        '05'      => 'Mei',
        '06'      => 'Juni',
        '07'      => 'Juli',
        '08'      => 'Agustus',
        '09'      => 'September',
        '10'      => 'Oktober',
        '11'      => 'Nopember',
        '12'      => 'Desember',
    );
    if($this->input->post(NULL)){
      $pst = $this->input->post(NULL);
      
//      $saldo_debit
      foreach($pst['debit_price'] AS $kdp => $dp){
        $just_debit = $this->global_models->string_to_number($dp);
        $saldo_debit += $just_debit;
        
        $debit[] = $just_debit;
        $debit_da[] = $pst['da'][$kdp];
      }
      foreach($pst['kredit_price'] AS $kkp => $kp){
        $just_kredit = $this->global_models->string_to_number($kp);
        $saldo_kredit += $just_kredit;
        
        $kredit[] = $just_kredit;
        $kredit_da[] = $pst['ka'][$kkp];
      }
      if($saldo_debit == $saldo_kredit){
        if($pst['id_detail']){
          $kirim = array(
              "id_keuangan_yayasan_journal"      => $id_keuangan_yayasan_journal,
              "notransaksi"         => $pst['notransaksi'],
              "saldo"               => $saldo_debit,
              "status"              => 1,
              "tanggal"             => $pst['tanggal'],
              "note"                => $pst['note'],
              "update_by_users"     => $this->session->userdata("id")
          );
          $id_keuangan_yayasan_journal_log = $this->global_models->update("keuangan_yayasan_journal_log_penyesuaian", array("id_keuangan_yayasan_journal_log_penyesuaian" => $pst['id_detail']),$kirim);
          
          foreach($pst['debit_price'] AS $key => $dp){
            $kirim_log_account[] = array(
                "id_frm_account"    => $pst['da'][$key],
                "id_keuangan_yayasan_journal"    => $id_keuangan_yayasan_journal,
                "id_keuangan_yayasan_journal_log_penyesuaian" => $pst['id_detail'],
                "saldo"             => $this->global_models->string_to_number($dp),
                "pos"               => 1,
                "create_by_users"     => $this->session->userdata("id"),
                "create_date"         => date("Y-m-d H:i:s")
            );
          }
          foreach($pst['kredit_price'] AS $key => $kp){
            $kirim_log_account[] = array(
                "id_frm_account"    => $pst['ka'][$key],
                "id_keuangan_yayasan_journal"    => $id_keuangan_yayasan_journal,
                "id_keuangan_yayasan_journal_log_penyesuaian"    => $pst['id_detail'],
                "saldo"             => $this->global_models->string_to_number($kp),
                "pos"               => 2,
                "create_by_users"     => $this->session->userdata("id"),
                "create_date"         => date("Y-m-d H:i:s")
            );
          }
          $this->global_models->delete("keuangan_yayasan_journal_log_account_penyesuaian", array("id_keuangan_yayasan_journal" => $id_keuangan_yayasan_journal, "id_keuangan_yayasan_journal_log_penyesuaian" => $pst['id_detail']));
          $this->global_models->insert_batch("keuangan_yayasan_journal_log_account_penyesuaian", $kirim_log_account);
          $this->session->set_flashdata('success', 'Data tersimpan');
            
        }
        else{
          $kirim = array(
              "id_keuangan_yayasan_journal"      => $id_keuangan_yayasan_journal,
              "notransaksi"         => $pst['notransaksi'],
              "saldo"               => $saldo_debit,
              "tanggal"             => $pst['tanggal'],
              "note"                => $pst['note'],
          );
          $id_keuangan_yayasan_journal_log_penyesuaian = $this->mfrm->create_journal_log_yayasan($kirim, "keuangan_yayasan_journal_log_penyesuaian");
        
          if($id_keuangan_yayasan_journal_log_penyesuaian){
            
            $this->mfrm->create_journal_log_account_yayasan($id_keuangan_yayasan_journal, $id_keuangan_yayasan_journal_log_penyesuaian, $debit, $debit_da, $kredit, $kredit_da, "_penyesuaian");
            
            $this->session->set_flashdata('success', 'Data tersimpan');
          }
          else{
            $this->session->set_flashdata('notice', 'Data tidak tersimpan');
          }
        }
      }
      else{
        $this->session->set_flashdata('notice', 'Saldo debit dan kredit tidak sama');
      }
      redirect("keuangan-yayasan/journal-log-penyesuaian/{$id_keuangan_yayasan_journal}");
    }
    else{
      $detail = $this->global_models->get("keuangan_yayasan_journal_log_penyesuaian", array("id_keuangan_yayasan_journal_log_penyesuaian" => $id_keuangan_yayasan_journal_log_penyesuaian));
      if($detail[0]->id_keuangan_yayasan_journal_log_penyesuaian){
        $keuangan_yayasan_journal_log_accoutn = $this->global_models->get_query("
          SELECT A.*, B.title AS account
          FROM keuangan_yayasan_journal_log_account_penyesuaian AS A
          LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
          WHERE id_keuangan_yayasan_journal_log_penyesuaian = {$id_keuangan_yayasan_journal_log_penyesuaian}");
        $no_debit = 0;
        $no_kredit = 0;
        foreach($keuangan_yayasan_journal_log_accoutn as $fjla){
          if($fjla->pos == 1){
            $tr['debit'][] = 'trdebit_'.$no_debit;
            $string['debit'][] = $fjla->account;
            $price_value['debit'][] = $fjla->saldo;
            $value_id['debit'][] = $fjla->id_frm_account;
            $no_debit++;
          }
          else{
            $tr['kredit'][] = 'trkredit_'.$no_kredit;
            $string['kredit'][] = $fjla->account;
            $price_value['kredit'][] = $fjla->saldo;
            $value_id['kredit'][] = $fjla->id_frm_account;
            $no_kredit++;
          }
        }
      }
      else{
        $tr = array(
            "debit"   => array('trdebit_1'),
            "kredit"  => array('trkredit_1')
        );
      }
      
      $this->template->build("add-journal-log-penyesuaian",
        array(
              'url'         => base_url()."themes/".DEFAULTTHEMES."/",
              'menu'        => 'keuangan-yayasan/journal',
              'title'       => lang("frm_journal_transaksi_penyesuaian"),
              'data'        => $detail,
              'bulan'       => $bulan,
              'id_keuangan_yayasan_journal' => $id_keuangan_yayasan_journal,
              'tr'          => $tr,
              'string'      => $string,
              'price_value' => $price_value,
              'value_id'    => $value_id,
            ));
      $this->template
        ->set_layout('default')
        ->build("add-journal-log-penyesuaian");
    }
	}
	
  public function journal(){
    
    $total = $this->global_models->get_query("
      SELECT count(A.id_keuangan_yayasan_journal) AS jumlah
      FROM keuangan_yayasan_journal AS A
      ");
      
    $this->template->build("journal",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-yayasan/journal',
            'title'       => lang("frm_journal"),
            'total'       => $total[0]->jumlah
          ));
    $this->template
      ->set_layout('default')
      ->build("journal");
	}
  
  public function journal_log($id_keuangan_yayasan_journal){
    
    $total = $this->global_models->get_query("
      SELECT count(A.id_keuangan_yayasan_journal_log) AS jumlah
      FROM keuangan_yayasan_journal_log AS A
      WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
      ");
      
    $this->template->build("journal-log",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-yayasan/journal',
            'title'       => lang("frm_journal_transaksi")." ".$this->global_models->get_field("keuangan_yayasan_journal", "title", array("id_keuangan_yayasan_journal" => $id_keuangan_yayasan_journal)),
            'total'       => $total[0]->jumlah,
            'id_keuangan_yayasan_journal' => $id_keuangan_yayasan_journal
          ));
    $this->template
      ->set_layout('default')
      ->build("journal-log");
	}
  
  public function journal_log_penyesuaian($id_keuangan_yayasan_journal){
    
    $total = $this->global_models->get_query("
      SELECT count(A.id_keuangan_yayasan_journal_log_penyesuaian) AS jumlah
      FROM keuangan_yayasan_journal_log_penyesuaian AS A
      WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
      ");
      
    $this->template->build("journal-log-penyesuaian",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-yayasan/journal',
            'title'       => lang("frm_journal_transaksi_penyesuaian")." ".$this->global_models->get_field("frm_journal", "title", array("id_frm_journal" => $id_frm_journal)),
            'total'       => $total[0]->jumlah,
            'id_keuangan_yayasan_journal' => $id_keuangan_yayasan_journal
          ));
    $this->template
      ->set_layout('default')
      ->build("journal-log-penyesuaian");
	}
  
  public function all_buku_besar($id_keuangan_yayasan_journal){
    $data = $this->global_models->get("frm_account");
    $this->template->build("all-buku-besar",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-yayasan/journal',
            'title'       => lang("frm_buku_besar"),
            'data'        => $data,
            'id_keuangan_yayasan_journal' => $id_keuangan_yayasan_journal
          ));
    $this->template
      ->set_layout('default')
      ->build("all-buku-besar");
  }
  
  public function work_sheet($id_keuangan_yayasan_journal){
    $neraca_percobaan = $this->global_models->get_query("
      SELECT A.title, A.nomor, B.*, A.pos, A.labarugi, A.id_frm_account
      FROM frm_account AS A
      LEFT JOIN keuangan_yayasan_neraca_percobaan AS B ON (B.id_frm_account = A.id_frm_account AND B.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal})
      ORDER BY A.nomor ASC
      ");
    $neraca_penyesuaian = $this->global_models->get_query("
      SELECT A.title, A.nomor, B.*, A.pos
      FROM frm_account AS A
      LEFT JOIN keuangan_yayasan_neraca_penyesuaian AS B ON (B.id_frm_account = A.id_frm_account AND B.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal})
      ORDER BY A.nomor ASC
      ");
    
//    $pajak = $this->global_models->get_query("
//      SELECT A.title, A.nomor, B.*, A.pos
//      FROM frm_account_pajak AS A
//      LEFT JOIN keuangan_yayasan_neraca_pajak AS B ON (B.id_frm_account_pajak = A.id_frm_account_pajak AND B.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal})
//      ORDER BY A.nomor ASC
//      ");
      
    $neraca = array(
        "percobaan"       => $neraca_percobaan,
        "penyesuaian"     => $neraca_penyesuaian,
        "pajak"           => $pajak,
    );
    
//    $this->debug($neraca, true);
    
    $this->template->build("work-sheet",
      array(
            'url'         => base_url()."themes/srabon2/",
            'theme2nd'    => 'srabon2',
            'menu'        => 'keuangan-yayasan/journal',
            'title'       => lang("frm_work_sheet"),
            'id_keuangan_yayasan_journal' => $id_keuangan_yayasan_journal,
            'neraca'      => $neraca
          ));
    $this->template
      ->set_layout('default')
      ->build("work-sheet");
  }
  
  public function neraca($id_keuangan_yayasan_journal){
    $data = $this->global_models->get_query("
      SELECT A.saldo AS price, A.*, B.title, B.nomor, B.pos AS pos_counter
      FROM keuangan_yayasan_neraca_percobaan AS A
      LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
      WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
      GROUP BY A.id_frm_account, A.pos
      ORDER BY B.nomor ASC");
//    $this->debug($data);
    foreach($data as $dt){
      if(!key_exists($dt->nomor, $baru)){
        $baru[$dt->nomor] = $dt;
        if($dt->pos != $dt->pos_counter){
          $baru[$dt->nomor]->price = $baru[$dt->nomor]->price * -1;
        }
      }
      else{
//        die;
        if($dt->pos != $dt->pos_counter){
          $baru[$dt->nomor]->price = ($dt->price * -1) + $baru[$dt->nomor]->price;
        }
        else{
          $baru[$dt->nomor]->price = $dt->price + $baru[$dt->nomor]->price;
        }
      }
    }
    
//    $this->debug($baru, true);
    
    $this->template->build("neraca",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-yayasan/journal',
            'title'       => lang("frm_neraca"),
            'data'        => $baru,
            'id_keuangan_yayasan_journal' => $id_keuangan_yayasan_journal
          ));
    $this->template
      ->set_layout('default')
      ->build("neraca");
  }
  
  public function rugi_laba($id_keuangan_yayasan_journal){
    $data = $this->global_models->get_query("
      SELECT A.saldo AS price, A.*, B.title, B.nomor, B.pos AS pos_counter
      FROM keuangan_yayasan_neraca_percobaan AS A
      LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
      WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
        AND B.labarugi = 2 AND B.profit = 1
      GROUP BY A.id_frm_account, A.pos
      ORDER BY B.nomor ASC");
    $data2 = $this->global_models->get_query("
      SELECT A.saldo AS price, A.*, B.title, B.nomor, B.pos AS pos_counter
      FROM keuangan_yayasan_neraca_penyesuaian AS A
      LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
      WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
        AND B.labarugi = 2 AND B.profit = 1
      GROUP BY A.id_frm_account, A.pos
      ORDER BY B.nomor ASC");
//    $this->debug($data);
    foreach($data as $dt){
      if(!key_exists($dt->nomor, $baru)){
        $baru[$dt->nomor] = $dt;
        if($dt->pos != $dt->pos_counter){
          $baru[$dt->nomor]->price = $baru[$dt->nomor]->price * -1;
        }
      }
      else{
//        die;
        if($dt->pos != $dt->pos_counter){
          $baru[$dt->nomor]->price = ($dt->price * -1) + $baru[$dt->nomor]->price;
        }
        else{
          $baru[$dt->nomor]->price = $dt->price + $baru[$dt->nomor]->price;
        }
      }
    }
    
    foreach($data2 as $dt){
      if(!key_exists($dt->nomor, $baru)){
        $baru[$dt->nomor] = $dt;
        if($dt->pos != $dt->pos_counter){
          $baru[$dt->nomor]->price = $baru[$dt->nomor]->price * -1;
        }
      }
      else{
//        die;
        if($dt->pos != $dt->pos_counter){
          $baru[$dt->nomor]->price = ($dt->price * -1) + $baru[$dt->nomor]->price;
        }
        else{
          $baru[$dt->nomor]->price = $dt->price + $baru[$dt->nomor]->price;
        }
      }
    }
    
    $data_fix = $this->global_models->get_query("
      SELECT A.saldo AS price, A.*, B.title, B.nomor, B.pos AS pos_counter
      FROM keuangan_yayasan_neraca_percobaan AS A
      LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
      WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
        AND B.labarugi = 2 AND B.profit = 2
      GROUP BY A.id_frm_account, A.pos
      ORDER BY B.nomor ASC");
    $data_fix2 = $this->global_models->get_query("
      SELECT A.saldo AS price, A.*, B.title, B.nomor, B.pos AS pos_counter
      FROM keuangan_yayasan_neraca_penyesuaian AS A
      LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
      WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
        AND B.labarugi = 2 AND B.profit = 2
      GROUP BY A.id_frm_account, A.pos
      ORDER BY B.nomor ASC");
//    $this->debug($data);
    foreach($data_fix as $dt_fix){
      if(!key_exists($dt_fix->nomor, $baru_fix)){
        $baru_fix[$dt_fix->nomor] = $dt_fix;
        if($dt_fix->pos != $dt_fix->pos_counter){
          $baru_fix[$dt_fix->nomor]->price = $baru_fix[$dt_fix->nomor]->price * -1;
        }
      }
      else{
//        die;
        if($dt_fix->pos != $dt_fix->pos_counter){
          $baru_fix[$dt_fix->nomor]->price = ($dt_fix->price * -1) + $baru_fix[$dt_fix->nomor]->price;
        }
        else{
          $baru_fix[$dt_fix->nomor]->price = $dt_fix->price + $baru_fix[$dt_fix->nomor]->price;
        }
      }
    }
    foreach($data_fix2 as $dt_fix){
      if(!key_exists($dt_fix->nomor, $baru_fix)){
        $baru_fix[$dt_fix->nomor] = $dt_fix;
        if($dt_fix->pos != $dt_fix->pos_counter){
          $baru_fix[$dt_fix->nomor]->price = $baru_fix[$dt_fix->nomor]->price * -1;
        }
      }
      else{
//        die;
        if($dt_fix->pos != $dt_fix->pos_counter){
          $baru_fix[$dt_fix->nomor]->price = ($dt_fix->price * -1) + $baru_fix[$dt_fix->nomor]->price;
        }
        else{
          $baru_fix[$dt_fix->nomor]->price = $dt_fix->price + $baru_fix[$dt_fix->nomor]->price;
        }
      }
    }
    
//    $this->debug($baru, true);
    
    $this->template->build("rugilaba",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-yayasan/journal',
            'title'       => lang("frm_rugi_laba"),
            'data'        => $baru,
            'data_fix'    => $baru_fix,
            'id_keuangan_yayasan_journal' => $id_keuangan_yayasan_journal
          ));
    $this->template
      ->set_layout('default')
      ->build("rugilaba");
  }
  
  public function neraca_penyesuaian($id_keuangan_yayasan_journal){
    $data = $this->global_models->get_query("
      SELECT A.saldo AS price, A.*, B.title, B.nomor, B.pos AS pos_counter
      FROM keuangan_yayasan_neraca_penyesuaian AS A
      LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
      WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
      GROUP BY A.id_frm_account, A.pos
      ORDER BY B.nomor ASC");
//    $this->debug($data);
    foreach($data as $dt){
      if(!key_exists($dt->nomor, $baru)){
        $baru[$dt->nomor] = $dt;
        if($dt->pos != $dt->pos_counter){
          $baru[$dt->nomor]->price = $baru[$dt->nomor]->price * -1;
        }
      }
      else{
//        die;
        if($dt->pos != $dt->pos_counter){
          $baru[$dt->nomor]->price = ($dt->price * -1) + $baru[$dt->nomor]->price;
        }
        else{
          $baru[$dt->nomor]->price = $dt->price + $baru[$dt->nomor]->price;
        }
      }
    }
    
//    $this->debug($baru, true);
    
    $this->template->build("neraca-penyesuaian",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-yayasan/journal',
            'title'       => lang("frm_neraca_penyesuaian"),
            'data'        => $baru,
            'id_keuangan_yayasan_journal' => $id_keuangan_yayasan_journal
          ));
    $this->template
      ->set_layout('default')
      ->build("neraca-penyesuaian");
  }
  
  public function neraca_setelah_penyesuaian($id_keuangan_yayasan_journal){
    $data2 = $this->global_models->get_query("
      SELECT A.saldo AS price, A.*, B.title, B.nomor, B.pos AS pos_counter, B.labarugi
      FROM keuangan_yayasan_neraca_penyesuaian AS A
      LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
      WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
      GROUP BY A.id_frm_account, A.pos
      ORDER BY B.nomor ASC");
    $data = $this->global_models->get_query("
      SELECT A.saldo AS price, A.*, B.title, B.nomor, B.pos AS pos_counter, B.labarugi
      FROM keuangan_yayasan_neraca_percobaan AS A
      LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
      WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
      GROUP BY A.id_frm_account, A.pos
      ORDER BY B.nomor ASC");
//    $this->debug($data, true);
    foreach($data as $dt){
      if(!key_exists($dt->nomor, $baru)){
        $baru[$dt->nomor] = $dt;
        if($dt->pos != $dt->pos_counter){
          $baru[$dt->nomor]->price = $baru[$dt->nomor]->price * -1;
        }
      }
      else{
//        die;
        if($dt->pos != $dt->pos_counter){
          $baru[$dt->nomor]->price = ($dt->price * -1) + $baru[$dt->nomor]->price;
        }
        else{
          $baru[$dt->nomor]->price = $dt->price + $baru[$dt->nomor]->price;
        }
      }
    }
    foreach($data2 as $dt){
      if(!key_exists($dt->nomor, $baru)){
        $baru[$dt->nomor] = $dt;
        if($dt->pos != $dt->pos_counter){
          $baru[$dt->nomor]->price = $baru[$dt->nomor]->price * -1;
        }
      }
      else{
//        die;
        if($dt->pos != $dt->pos_counter){
          $baru[$dt->nomor]->price = ($dt->price * -1) + $baru[$dt->nomor]->price;
        }
        else{
          $baru[$dt->nomor]->price = $dt->price + $baru[$dt->nomor]->price;
        }
      }
    }
    
//    $this->debug($baru, true);
    
    $this->template->build("neraca-setelah-penyesuaian",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-yayasan/journal',
            'title'       => lang("frm_neraca_setelah_penyesuaian"),
            'data'        => $baru,
            'id_keuangan_yayasan_journal' => $id_keuangan_yayasan_journal
          ));
    $this->template
      ->set_layout('default')
      ->build("neraca-setelah-penyesuaian");
  }
  
  public function buku_besar($id_keuangan_yayasan_journal, $id_frm_account){
    
    $total = $this->global_models->get_query("
      SELECT count(A.id_keuangan_yayasan_journal_log_account) AS jumlah
      FROM keuangan_yayasan_journal_log_account AS A
      WHERE A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal} AND A.id_frm_account = {$id_frm_account}
      ");
      
    $this->template->build("buku-besar",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-yayasan/journal',
            'title'       => lang("frm_buku_besar")." ".
          $this->global_models->get_field("frm_account", "title", array("id_frm_account" => $id_frm_account))." ".
          $this->global_models->get_field("keuangan_yayasan_journal", "title", array("id_keuangan_yayasan_journal" => $id_keuangan_yayasan_journal)),
            'total'       => $total[0]->jumlah,
            'id_keuangan_yayasan_journal' => $id_keuangan_yayasan_journal,
            'id_frm_account' => $id_frm_account,
          ));
    $this->template
      ->set_layout('default')
      ->build("buku-besar");
	}
  
  public function ajax_journal($total = 0, $start = 0){
    $bulan = array(
        '1'      => 'Januari',
        '2'      => 'Februari',
        '3'      => 'Maret',
        '4'      => 'April',
        '5'      => 'Mei',
        '6'      => 'Juni',
        '7'      => 'Juli',
        '8'      => 'Agustus',
        '9'      => 'September',
        '10'      => 'Oktober',
        '11'      => 'Nopember',
        '12'      => 'Desember',
    );
    
    $data = $this->global_models->get_query("
      SELECT A.*
      FROM keuangan_yayasan_journal AS A
      ORDER BY A.sort LIMIT {$start},10
      ");
    
    foreach ($data as $key => $value) {
      if($this->global_models->get_field("keuangan_yayasan_journal", "status", array("id_keuangan_yayasan_journal" => $value->id_keuangan_yayasan_journal)) == 2)
        $gabungan = "<li><a href='".site_url("keuangan-yayasan/gabungan/neraca/".$value->id_keuangan_yayasan_journal)."'>Neraca Gabungan</a></li>";
      else
        $gabungan = "";
      print "
      <tr class='ajax-quotation'>
        <td>{$value->title}</td>
        <td>{$bulan[$value->month]}</td>
        <td>{$value->year}</td>
        <td>
          <div class='btn-group'>
            <button data-toggle='dropdown' class='btn btn-small dropdown-toggle'>Action<span class='caret'></span></button>
            <ul class='dropdown-menu'>
              <li><a href='".site_url("keuangan-yayasan/journal-log/".$value->id_keuangan_yayasan_journal)."'>Journal</a></li>
              {$gabungan}
            </ul>
          </div>
        </td>
      </tr>";
        
    }
    
    $this->load->library('pagination');

    $config['base_url'] = '';
    $config['total_rows'] = $total;
    $config['per_page'] = 10; 
    $config['uri_segment'] = 4; 
    $config['cur_tag_open'] = "<li class='active'><a href='javascript:void(0)'>"; 
    $config['cur_tag_close'] = "</a></li>"; 
    $config['first_tag_open'] = "<li>"; 
    $config['first_tag_close'] = "</li>"; 
    $config['last_tag_open'] = "<li>"; 
    $config['last_tag_close'] = "</li>"; 
    $config['next_tag_open'] = "<li>"; 
    $config['next_tag_close'] = "</li>"; 
    $config['prev_tag_open'] = "<li>"; 
    $config['prev_tag_close'] = "</li>"; 
    $config['num_tag_open'] = "<li>"; 
    $config['num_tag_close'] = "</li>";
    $config['function_js'] = "load_quotation";

    $this->pagination->initialize($config); 
    print "
    <tr class='ajax-quotation'>
      <td colspan='6'>
        <div class='pagination'>
          <ul>
          ".$this->pagination->create_links_ajax()."
          </ul>
        </div>
      </td>
    </tr>";
    die;
  }
  
  public function ajax_journal_log($id_keuangan_yayasan_journal, $total = 0, $start = 0){
    
    $data = $this->global_models->get_query("
      SELECT A.*
      FROM keuangan_yayasan_journal_log AS A
      WHERE id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
      ORDER BY A.tanggal LIMIT {$start},10
      ");
//    $this->debug("SELECT A.*
//      FROM keuangan_yayasan_journal_log AS A
//      WHERE id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
//      ORDER BY A.tanggal LIMIT {$start},10", true);
    foreach ($data as $key => $value) {
      $keuangan_yayasan_journal_log_account = $this->global_models->get_query("
        SELECT A.*, B.title AS account, B.nomor AS account_nomor
        FROM keuangan_yayasan_journal_log_account AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_keuangan_yayasan_journal_log = {$value->id_keuangan_yayasan_journal_log}
          ORDER BY B.nomor ASC
        ");
        
      foreach ($keuangan_yayasan_journal_log_account AS $fjla){
        if($fjla->pos == 1){
          $account_debit = "<a href='".site_url("keuangan-yayasan/buku-besar/".$id_keuangan_yayasan_journal."/".$fjla->id_frm_account)."'>".$fjla->account."</a>";
          $account_kredit = "";
          
          $saldo_debit = $fjla->saldo;
          $saldo_kredit = "";
        }
        else{
          $account_debit = "";
          $account_kredit = "<a href='".site_url("keuangan-yayasan/buku-besar/".$id_keuangan_yayasan_journal."/".$fjla->id_frm_account)."'>".$fjla->account."</a>";
          
          $saldo_debit = "";
          $saldo_kredit = $fjla->saldo;
        }
        print "
        <tr class='ajax-quotation'>
          <td>{$account_debit}</td>
          <td>{$account_kredit}</td>
          <td>{$fjla->account_nomor}</td>
          <td style='text-align: right'>Rp ".  number_format($saldo_debit, 0, ",", ".")."</td>
          <td style='text-align: right'>Rp ".  number_format($saldo_kredit, 0, ",", ".")."</td>
        </tr>";
      }
      
      print "
      <tr class='ajax-quotation' style='background-color: rgba(128, 128, 128, 0.33); font-weight: bold;'>
        <td colspan='2'>{$value->tanggal} | {$value->note}</td>
        <td>{$value->notransaksi}</td>
        <td colspan='2' style='text-align: right'>Rp ".  number_format($value->saldo, 0, ",", ".")."</td>
      </tr>";
//        <td style='text-align: right'>
//          <div class='btn-group'>
//            <button data-toggle='dropdown' class='btn btn-small dropdown-toggle'>Action<span class='caret'></span></button>
//            <ul class='dropdown-menu'>
//              <li><a href='".site_url("frm/accounting/journal-koreksi/{$id_frm_journal}/".$value->id_frm_journal_log)."'>Journal Koreksi</a></li>
//            </ul>
//          </div>
//        </td>
    }
    
    $this->load->library('pagination');

    $config['base_url'] = '';
    $config['total_rows'] = $total;
    $config['per_page'] = 10; 
    $config['uri_segment'] = 6; 
    $config['cur_tag_open'] = "<li class='active'><a href='javascript:void(0)'>"; 
    $config['cur_tag_close'] = "</a></li>"; 
    $config['first_tag_open'] = "<li>"; 
    $config['first_tag_close'] = "</li>"; 
    $config['last_tag_open'] = "<li>"; 
    $config['last_tag_close'] = "</li>"; 
    $config['next_tag_open'] = "<li>"; 
    $config['next_tag_close'] = "</li>"; 
    $config['prev_tag_open'] = "<li>"; 
    $config['prev_tag_close'] = "</li>"; 
    $config['num_tag_open'] = "<li>"; 
    $config['num_tag_close'] = "</li>";
    $config['function_js'] = "load_quotation";

    $this->pagination->initialize($config); 
    print "
    <tr class='ajax-quotation'>
      <td colspan='6'>
        <div class='pagination'>
          <ul>
          ".$this->pagination->create_links_ajax()."
          </ul>
        </div>
      </td>
    </tr>";
    die;
  }
  
  public function ajax_journal_log_penyesuaian($id_keuangan_yayasan_journal, $total = 0, $start = 0){
    
    $data = $this->global_models->get_query("
      SELECT A.*
      FROM keuangan_yayasan_journal_log_penyesuaian AS A
      WHERE id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
      ORDER BY A.tanggal LIMIT {$start},10
      ");
    
    foreach ($data as $key => $value) {
      $keuangan_yayasan_journal_log_account = $this->global_models->get_query("
        SELECT A.*, B.title AS account, B.nomor AS account_nomor
        FROM keuangan_yayasan_journal_log_account_penyesuaian AS A
        LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
        WHERE A.id_keuangan_yayasan_journal_log_penyesuaian = {$value->id_keuangan_yayasan_journal_log_penyesuaian}");
        
      foreach ($keuangan_yayasan_journal_log_account AS $fjla){
        if($fjla->pos == 1){
          $account_debit = "<a href='".site_url("keuangan-yayasan/buku-besar/".$id_keuangan_yayasan_journal."/".$fjla->id_frm_account)."'>".$fjla->account."</a>";
          $account_kredit = "";
          
          $saldo_debit = $fjla->saldo;
          $saldo_kredit = "";
        }
        else{
          $account_debit = "";
          $account_kredit = "<a href='".site_url("keuangan-yayasan/buku-besar/".$id_keuangan_yayasan_journal."/".$fjla->id_frm_account)."'>".$fjla->account."</a>";
          
          $saldo_debit = "";
          $saldo_kredit = $fjla->saldo;
        }
        print "
        <tr class='ajax-quotation'>
          <td>{$account_debit}</td>
          <td>{$account_kredit}</td>
          <td>{$fjla->account_nomor}</td>
          <td style='text-align: right'>Rp ".  number_format($saldo_debit, 0, ",", ".")."</td>
          <td style='text-align: right'>Rp ".  number_format($saldo_kredit, 0, ",", ".")."</td>
        </tr>";
      }
      
      print "
      <tr class='ajax-quotation' style='background-color: rgba(128, 128, 128, 0.33); font-weight: bold;'>
        <td colspan='2'>{$value->tanggal} | {$value->note}</td>
        <td>{$value->notransaksi}</td>
        <td style='text-align: right'>Rp ".  number_format($value->saldo, 0, ",", ".")."</td>
        <td style='text-align: right'>
          
        </td>
      </tr>";
        
    }
    
    $this->load->library('pagination');

    $config['base_url'] = '';
    $config['total_rows'] = $total;
    $config['per_page'] = 10; 
    $config['uri_segment'] = 6; 
    $config['cur_tag_open'] = "<li class='active'><a href='javascript:void(0)'>"; 
    $config['cur_tag_close'] = "</a></li>"; 
    $config['first_tag_open'] = "<li>"; 
    $config['first_tag_close'] = "</li>"; 
    $config['last_tag_open'] = "<li>"; 
    $config['last_tag_close'] = "</li>"; 
    $config['next_tag_open'] = "<li>"; 
    $config['next_tag_close'] = "</li>"; 
    $config['prev_tag_open'] = "<li>"; 
    $config['prev_tag_close'] = "</li>"; 
    $config['num_tag_open'] = "<li>"; 
    $config['num_tag_close'] = "</li>";
    $config['function_js'] = "load_quotation";

    $this->pagination->initialize($config); 
    print "
    <tr class='ajax-quotation'>
      <td colspan='6'>
        <div class='pagination'>
          <ul>
          ".$this->pagination->create_links_ajax()."
          </ul>
        </div>
      </td>
    </tr>";
    die;
  }
  
  public function ajax_buku_besar($id_keuangan_yayasan_journal, $id_frm_account, $total = 0, $start = 0){
    
    $data = $this->global_models->get_query("
      SELECT A.*, B.notransaksi, B.note, B.tanggal
      FROM keuangan_yayasan_journal_log_account AS A
      LEFT JOIN keuangan_yayasan_journal_log AS B ON A.id_keuangan_yayasan_journal_log = B.id_keuangan_yayasan_journal_log
      WHERE A.id_frm_account = {$id_frm_account} AND A.id_keuangan_yayasan_journal = {$id_keuangan_yayasan_journal}
      ORDER BY B.tanggal DESC LIMIT {$start},10
      ");
    $total_debit = $this->global_models->get_query("
      SELECT SUM(A.saldo) AS saldo
      FROM keuangan_yayasan_journal_log_account AS A
      LEFT JOIN keuangan_yayasan_journal_log AS B ON A.id_keuangan_yayasan_journal_log = B.id_keuangan_yayasan_journal_log
      WHERE A.id_frm_account = $id_frm_account AND A.id_keuangan_yayasan_journal = $id_keuangan_yayasan_journal AND A.pos = 1 AND B.tanggal <= '{$data[0]->tanggal}'
      ");
      
    $total_kredit = $this->global_models->get_query("
      SELECT SUM(A.saldo) AS saldo
      FROM keuangan_yayasan_journal_log_account AS A
      LEFT JOIN keuangan_yayasan_journal_log AS B ON A.id_keuangan_yayasan_journal_log = B.id_keuangan_yayasan_journal_log
      WHERE A.id_frm_account = $id_frm_account AND A.id_keuangan_yayasan_journal = $id_keuangan_yayasan_journal AND A.pos = 2 AND B.tanggal <= '{$data[0]->tanggal}'
      ");
//    $this->debug($total_debit);
//    $this->debug($total_kredit, true);
//    $total_kredit = $this->global_models->get_field("frm_journal_log_account", "SUM(saldo)", 
//            array("id_frm_account" => $id_frm_account, "id_frm_journal" => $id_frm_journal, "pos" => 2, "tanggal >" => $data[0]->tanggal));
    
    $pos_asli = $this->global_models->get_field("frm_account", "pos", array("id_frm_account" => $id_frm_account));
    if($pos_asli == 1)
      $total_akhir = $total_debit[0]->saldo - $total_kredit[0]->saldo;
    else
      $total_akhir = $total_kredit[0]->saldo - $total_debit[0]->saldo;
    
    foreach ($data as $key => $value) {
      if($value->pos == 1){
        $saldo_debit = $value->saldo;
        $saldo_kredit = "";
        
      }
      else{
        $saldo_debit = "";
        $saldo_kredit = $value->saldo;
        
      }
      print "
      <tr class='ajax-quotation'>
        <td>{$value->tanggal}</td>
        <td>{$value->note}</td>
        <td>{$value->notransaksi}</td>
        <td style='text-align: right'>Rp ".  number_format($saldo_debit, 0, ",", ".")."</td>
        <td style='text-align: right'>Rp ".  number_format($saldo_kredit, 0, ",", ".")."</td>
        <td style='text-align: right; font-weight: bold;'>Rp ".  number_format($total_akhir, 0, ",", ".")."</td>
      </tr>";
      
      if($value->pos == 1){
        if($pos_asli == 1){
          $total_akhir = $total_akhir - $saldo_debit;
        }
        else{
          $total_akhir = $total_akhir + $saldo_debit;
        }
      }
      else{
        if($pos_asli == 2){
          $total_akhir = $total_akhir - $saldo_kredit;
        }
        else{
          $total_akhir = $total_akhir + $saldo_kredit;
        }
      }
    }
    
    $this->load->library('pagination');

    $config['base_url'] = '';
    $config['total_rows'] = $total;
    $config['per_page'] = 10; 
    $config['uri_segment'] = 7; 
    $config['cur_tag_open'] = "<li class='active'><a href='javascript:void(0)'>"; 
    $config['cur_tag_close'] = "</a></li>"; 
    $config['first_tag_open'] = "<li>"; 
    $config['first_tag_close'] = "</li>"; 
    $config['last_tag_open'] = "<li>"; 
    $config['last_tag_close'] = "</li>"; 
    $config['next_tag_open'] = "<li>"; 
    $config['next_tag_close'] = "</li>"; 
    $config['prev_tag_open'] = "<li>"; 
    $config['prev_tag_close'] = "</li>"; 
    $config['num_tag_open'] = "<li>"; 
    $config['num_tag_close'] = "</li>";
    $config['function_js'] = "load_quotation";

    $this->pagination->initialize($config); 
    print "
    <tr class='ajax-quotation'>
      <td colspan='6'>
        <div class='pagination'>
          <ul>
          ".$this->pagination->create_links_ajax()."
          </ul>
        </div>
      </td>
    </tr>";
    die;
  }
  
  public function close_month($id_keuangan_yayasan_journal){
    if($this->global_models->get_field("keuangan_yayasan_journal", "status", array("id_keuangan_yayasan_journal" => $id_keuangan_yayasan_journal)) != 1)
            redirect("keuangan-yayasan/work-sheet/".$id_keuangan_yayasan_journal);
    
    $account = $this->global_models->get_field("frm_account", "count(id_frm_account)");
    
    $lama = $this->global_models->get("keuangan_yayasan_journal", array("id_keuangan_yayasan_journal" => $id_keuangan_yayasan_journal));
    if($lama[0]->month < 12){
      $bulan = $lama[0]->month + 1;
      $year = $lama[0]->year;
    }
    else{
      $bulan = 1;
      $year = $lama[0]->year + 1;
    }
    $sort = $year."-".$bulan."-01";
    $kirim_journal = array(
        "title"         => date("F Y", strtotime($sort)),
        "month"         => $bulan,
        "year"          => $year,
        "sort"          => $sort,
        "status"        => 1,
        "create_by_users"       => $this->session->userdata("id"),
        "create_date"           => date("Y-m-d H:i:s")
    );
    $id_journal = $this->global_models->insert("keuangan_yayasan_journal", $kirim_journal);
    
    $this->session->set_userdata(array(
        "keuangan_yayasan_generate_close_month_jumlah_proses"  => ($account + 6),
        "keuangan_yayasan_generate_close_month_jumlah_account" => ($account - 1),
        "keuangan_yayasan_generate_close_month_count_proses"   => 0,
        "keuangan_yayasan_generate_close_month_id_keuangan_yayasan_journal" => $id_journal,
    ));
    
    $this->template->build("generate-close-month-batch",
      array(
            'url'         => base_url()."themes/".DEFAULTTHEMES."/",
            'menu'        => 'keuangan-yayasan/journal',
            'title'       => lang("frm_close_month"),
            'type'        => $id_keuangan_yayasan_journal, 
          ));
    $this->template
      ->set_layout('default')
      ->build("generate-close-month-batch");
  }
  
  public function batch_close_month_final($id_journal){
    $lama = $this->global_models->get("keuangan_yayasan_journal", array("id_keuangan_yayasan_journal" => $id_journal));
    if($lama[0]->month < 12){
      $bulan = $lama[0]->month + 1;
      $year = $lama[0]->year;
    }
    else{
      $bulan = 1;
      $year = $lama[0]->year + 1;
    }
    $sort = $year."-".$bulan."-01";
//    $kirim_journal = array(
//        "title"         => date("F Y", strtotime($sort)),
//        "month"         => $bulan,
//        "year"          => $year,
//        "sort"          => $sort,
//        "status"        => 1,
//        "create_by_users"       => $this->session->userdata("id"),
//        "create_date"           => date("Y-m-d H:i:s")
//    );
//    $id_frm_journal = $this->global_models->insert("frm_journal", $kirim_journal);
    
    $this->global_models->update("keuangan_yayasan_journal", array("id_keuangan_yayasan_journal" => $id_journal), array("id_next" => $this->session->userdata("keuangan_yayasan_generate_close_month_id_keuangan_yayasan_journal"), "status" => 2));
    
    $keuangan_yayasan_pendapatan_sebelum_pajak = $this->nbscache->get_explode("variable", "keuangan_yayasan_pendapatan_sebelum_pajak");
    
    $data_log = $this->global_models->get_query("
      SELECT A.*, B.pos
      FROM keuangan_yayasan_journal_bukubesar AS A
      LEFT JOIN frm_account AS B ON A.id_frm_account = B.id_frm_account
      WHERE A.id_keuangan_yayasan_journal = {$id_journal} AND (B.labarugi <> 2 OR B.labarugi IS NULL)
      AND B.id_frm_account <> {$keuangan_yayasan_pendapatan_sebelum_pajak[1]}
      AND A.saldo <> 0
      ");
      
    $kirim_awal = array(
        "id_keuangan_yayasan_journal"        => $this->session->userdata("keuangan_yayasan_generate_close_month_id_keuangan_yayasan_journal"),
        "tanggal"               => $sort,
        "notransaksi"           => "",
        "saldo"                 => $saldo_all,
        "status"                => 1,
        "note"                  => "Close ".date("F Y", strtotime($lama[0]->sort)),
        "create_by_users"       => $this->session->userdata("id"),
        "create_date"           => date("Y-m-d H:i:s")
    );
    $id_keuangan_yayasan_journal_log = $this->global_models->insert("keuangan_yayasan_journal_log", $kirim_awal);
    
    $kirim_log[] = array(
          "id_frm_account"        => $keuangan_yayasan_pendapatan_sebelum_pajak[1],
          "id_keuangan_yayasan_journal"        => $this->session->userdata("keuangan_yayasan_generate_close_month_id_keuangan_yayasan_journal"),
          "id_keuangan_yayasan_journal_log"    => $id_keuangan_yayasan_journal_log,
          "saldo"                 => $this->global_models->get_field("keuangan_yayasan_journal_tutup", "saldo", array("id_keuangan_yayasan_journal" => $id_journal, "type" => 6)),
          "pos"                   => 2,
          "create_by_users"       => $this->session->userdata("id"),
          "create_date"           => date("Y-m-d H:i:s")
      );
    
    $kirim_neraca = array(
          "id_frm_account"        => $keuangan_yayasan_pendapatan_sebelum_pajak[1],
          "id_keuangan_yayasan_journal"        => $this->session->userdata("keuangan_yayasan_generate_close_month_id_keuangan_yayasan_journal"),
          "saldo"                 => $this->global_models->get_field("keuangan_yayasan_journal_tutup", "saldo", array("id_keuangan_yayasan_journal" => $id_journal, "type" => 6)),
          "create_by_users"       => $this->session->userdata("id"),
          "create_date"           => date("Y-m-d H:i:s"),
          "pos"                   => 2
      );
      
      $id_keuangan_yayasan_neraca_percobaan = $this->global_models->insert("keuangan_yayasan_neraca_percobaan", $kirim_neraca);
    
    foreach($data_log AS $dl){
      $kirim_log[] = array(
          "id_frm_account"        => $dl->id_frm_account,
          "id_keuangan_yayasan_journal"        => $this->session->userdata("keuangan_yayasan_generate_close_month_id_keuangan_yayasan_journal"),
          "id_keuangan_yayasan_journal_log"    => $id_keuangan_yayasan_journal_log,
          "saldo"                 => $dl->saldo,
          "pos"                   => $dl->pos,
          "create_by_users"       => $this->session->userdata("id"),
          "create_date"           => date("Y-m-d H:i:s")
      );
      if($dl->pos == 1)
        $saldo_all += $dl->saldo;
    }
    
    $this->global_models->update("keuangan_yayasan_journal_log", array("id_keuangan_yayasan_journal_log" => $id_keuangan_yayasan_journal_log), array("saldo" => $saldo_all));
    $this->global_models->insert_batch("keuangan_yayasan_journal_log_account", $kirim_log);
    
    die;
  }
  public function batch_close_month($id_journal){
    if($this->session->userdata("keuangan_yayasan_generate_close_month_count_proses") > 0){
      $nomor = $this->session->userdata("keuangan_yayasan_generate_close_month_count_proses") + 1;
    }
    else{
      $nomor = 1;
    }
    $keuangan_yayasan_pendapatan_sebelum_pajak = $this->nbscache->get_explode("variable", "keuangan_yayasan_pendapatan_sebelum_pajak");
    $proses_tambahan = array(
        "-1"    => "Total Pendapatan",
        "-2"    => "Total Lainnya",
        "-3"    => "Total Beban HPP",
        "-4"    => "Total Beban Lain",
        "-5"    => "Rugi Laba Kotor",
        "-6"    => "Rugi Laba Sebelum Pajak",
    );
    if($this->session->userdata("keuangan_yayasan_generate_close_month_jumlah_account") >= 0){
      $account = $this->global_models->get_query("SELECT * FROM frm_account LIMIT {$this->session->userdata("keuangan_yayasan_generate_close_month_jumlah_account")}, 1");
      $title = $account[0]->title;
      $saldo = $this->mfrm->get_saldo_buku_besar_yayasan($id_journal, $account[0]->id_frm_account);
      $kirim_buku_besar = array(
          "id_frm_account"        => $account[0]->id_frm_account,
          "id_keuangan_yayasan_journal"        => $id_journal,
          "saldo"                 => $saldo,
          "create_by_users"       => $this->session->userdata("id"),
          "create_date"           => date("Y-m-d H:i:s")
      );
      $id_keuangan_yayasan_journal_bukubesar = $this->global_models->insert("keuangan_yayasan_journal_bukubesar", $kirim_buku_besar);
      
      if($account[0]->labarugi != 2 AND $account[0]->id_frm_account != $keuangan_yayasan_pendapatan_sebelum_pajak[1]){
        $kirim_neraca = array(
            "id_frm_account"        => $account[0]->id_frm_account,
            "id_keuangan_yayasan_journal"        => $this->session->userdata("keuangan_yayasan_generate_close_month_id_keuangan_yayasan_journal"),
            "saldo"                 => $saldo,
            "create_by_users"       => $this->session->userdata("id"),
            "create_date"           => date("Y-m-d H:i:s"),
            "pos"                   => $account[0]->pos
        );

        $id_keuangan_yayasan_neraca_percobaan = $this->global_models->insert("keuangan_yayasan_neraca_percobaan", $kirim_neraca);
      }
      
    }
    else{
      $title = $proses_tambahan[$this->session->userdata("keuangan_yayasan_generate_close_month_jumlah_account")];
      switch ($this->session->userdata("keuangan_yayasan_generate_close_month_jumlah_account")) {
        case '-1':
          $saldo = $this->mfrm->get_total_pendapatan_yayasan($id_journal);
          $type_tutup = 1;
          break;
        case '-2':
          $saldo = $this->mfrm->get_total_pendapatan_lain_yayasan($id_journal);
          $type_tutup = 2;
          break;
        case '-3':
          $saldo = $this->mfrm->get_total_beban_yayasan($id_journal);
          $type_tutup = 3;
          break;
        case '-4':
          $saldo = $this->mfrm->get_total_beban_lain_yayasan($id_journal);
          $type_tutup = 4;
          break;
        case '-5':
          $saldo = $this->mfrm->get_pendapatan_kotor_yayasan($id_journal);
          $type_tutup = 5;
          break;
        case '-6':
          $saldo = $this->mfrm->get_pendapatan_sebelum_pajak_yayasan($id_journal);
          $type_tutup = 6;
          break;
        default:
          
          break;
      }
      $kirim_tutup = array(
          "id_keuangan_yayasan_journal"        => $id_journal,
          "type"                  => $type_tutup,
          "saldo"                 => $saldo,
          "create_by_users"       => $this->session->userdata("id"),
          "create_date"           => date("Y-m-d H:i:s")
      );
      $id_keuangan_yayasan_journal_tutup = $this->global_models->insert("keuangan_yayasan_journal_tutup", $kirim_tutup);
    }
    
    $set_session = array(
        "keuangan_yayasan_generate_close_month_count_proses"   => $nomor,
        "keuangan_yayasan_generate_close_month_jumlah_account" => ($this->session->userdata("keuangan_yayasan_generate_close_month_jumlah_account") - 1),
    );
    $this->session->set_userdata($set_session);
    $hasil = array(
        "percent" => ($nomor/$this->session->userdata("keuangan_yayasan_generate_close_month_jumlah_proses") * 100),
        "n"       => $nomor,
        "rn"      => $saldo,
        "note"    => $title
    );
    print json_encode($hasil);
    die;
  }
  
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */